package utopia_activities;

import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

import org.apache.http.impl.client.DefaultHttpClient;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.transform.RegistryMatcher;

import com.example.utopia.shoppinglist.R;
import com.example.utopia.shoppinglist.R.id;
import com.example.utopia.shoppinglist.R.layout;

import de.dhbw_stuttgart.wi2011i.outofapples.DateTransform;
import de.dhbw_stuttgart.wi2011i.outofapples.UUIDTransform;
import de.dhbw_stuttgart.wi2011i.outofapples.dtos.ShoppingList;
import de.dhbw_stuttgart.wi2011i.outofapples.logging.LoggerImpl;
import de.dhbw_stuttgart.wi2011i.outofapples.persistence.OnlineEinkaufsblock;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class logon_screen extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.logon_screen);
		ActionBar bar = getActionBar();
		bar.setDisplayHomeAsUpEnabled(true);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{    
	   switch (item.getItemId()) 
	   {        
	      case android.R.id.home:            
	         Intent intent = new Intent(this, Utopia.class);            
	         startActivity(intent);            
	         return true;        
	      default:            
	         return super.onOptionsItemSelected(item);    
	   }
	}
	
	public void OnButtonClick(View view){
    	if (view.getId()==R.id.logon_screen_btn_logon) {
    		final Context context = this;
            
    		EditText username= (EditText) findViewById(R.id.logon_screen_edit_email);
    		EditText password= (EditText) findViewById(R.id.logon_screen_password);
    		String user = username.getText().toString();
    		String pass = password.getText().toString();
    		
    		/*RegistryMatcher matcher = new RegistryMatcher();
	        
	        matcher.bind(UUID.class, new UUIDTransform());
	        matcher.bind(Date.class, new DateTransform(java.text.DateFormat.getDateInstance(java.text.DateFormat.DEFAULT, Locale.US)));
	        
	        Persister persister = new Persister(matcher);
	        OnlineEinkaufsblock block = new OnlineEinkaufsblock(new LoggerImpl(), persister, context, new DefaultHttpClient());
	        block.setUrl("http://www.matthiaswelz.de:8094/OutOfAPPles");
	        
	        block.tryLogin(user, pass);
	        
	        if (block.isLoggedIn()){
	        Toast.makeText(context, "sucessfully logged in", Toast.LENGTH_SHORT).show(); 
	        startActivity(new Intent(context, menu.class));
	        }*/
    		
	        Toast.makeText(context, user + " sucessfully logged in!", Toast.LENGTH_SHORT).show(); 
	        startActivity(new Intent(this, menu.class));
	        
    		
		} else if (view.getId()==R.id.logon_screen_btn_createaccount){
			startActivity(new Intent(this, create_account.class));
		} else if (view.getId()==R.id.logon_screen_btn_userguide){
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Hello, in order to start using this app please login by typing in the necessary information. " +
					"You can easily create an account if you have not registered. On the following pages you can create new shopping lists or select the ones you already have created." +
					"To create a new shoppinglist please type a name for the list and select an user with whom you want to share it ." +
					"By clicking on 'Select user' a new window will pop-up where you can select a user. In this pop-up windiw click 'invite' to add the user." +
					"When you are finished yo can click on 'create'. Add a new item by start typing the name. You can add details like amount, shop, price, brand, the username and a note." +
					"In the main window only the item's name and the selected user are shown. To see more details on a certain item you just click on '+'." +
					"In a pop-up window you will see the rest of the details. Only User that have been invited to the list can be selected. We you enjoy the Utopia Shopping list!")
			       .setCancelable(false)
			       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {}});
			AlertDialog alert = builder.create();
			alert.show();
		}
		
}
}
